const logger = require('./console-logger')
const ConfigModel = require('../models/config-model')
const cache = require('./cache.js')

module.exports = {
    me: async (force) => {
        const cacheKey = 'system-config';
        let systemConfig = force ? undefined : cache.get(cacheKey);
        logger.debug('SystemConfig.me cacheKey: ', cacheKey, cache.exists(cacheKey));
        if (!systemConfig) {
            systemConfig = await ConfigModel.get();
            if (systemConfig.notify && typeof systemConfig.notify == 'string') systemConfig.notify = JSON.parse(systemConfig.notify)
            if (systemConfig.jackett && typeof systemConfig.jackett == 'string') systemConfig.jackett = JSON.parse(systemConfig.jackett)
            if (systemConfig.tmdb && typeof systemConfig.tmdb == 'string') systemConfig.tmdb = JSON.parse(systemConfig.tmdb)
            if (systemConfig.jellyfin && typeof systemConfig.jellyfin == 'string') systemConfig.jellyfin = JSON.parse(systemConfig.jellyfin)
            cache.set(cacheKey, systemConfig);
        }
        return systemConfig;
    },
    clean() {
        cache.del('system-config');
    }
}